//
//  PhotoImage.swift
//  SwiftTest
//
//  Created by appleTwo on 2019/3/8.
//  Copyright © 2019 QiKai. All rights reserved.
//

import UIKit

@objc protocol PhotoDelegate:NSObjectProtocol {

    @objc optional func getImageDelegate(image:UIImage)
    
}
class PhotoImage: NSObject,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    // MARK: 属性
    var imagepick:UIImagePickerController!
    weak var delegate:PhotoDelegate?
    
    // MARK: 方法
    //单例
    static let shareManage = PhotoImage()
    override init() {
        super.init()
        //print("init")
        imagepick = UIImagePickerController.init()
        imagepick.delegate = self
        imagepick.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        imagepick.allowsEditing = true
    }
    
    func photoChoseCarmerOrphoto(viewcontroller:UIViewController) {
        //设置代理
//        if let delegateVc = viewcontroller as? PhotoDelegate {
//            self.delegate = delegateVc
//        }
        let alterselect = UIAlertController.init(title: "提示", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
        
        let altercamera = UIAlertAction.init(title: "拍照", style: UIAlertAction.Style.default) { (UIAlertAction) in
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
                self.imagepick.sourceType = UIImagePickerController.SourceType.camera
                viewcontroller.present(self.imagepick, animated: true, completion: nil)
                
            }else{
//               SVProgressHUD.showInfo(withStatus: "请开启照相机权限")
            }
        }
        let alterPhoto = UIAlertAction.init(title: "相册", style: UIAlertAction.Style.default) { (UIAlertAction) in
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
                self.imagepick.sourceType = UIImagePickerController.SourceType.photoLibrary
                viewcontroller.present(self.imagepick, animated: true, completion: nil)
                
            }else{
//                SVProgressHUD.showInfo(withStatus: "请开启相册权限")
            }
        }
        let alterCancle = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel) { (UIAlertAction) in
            
        }
        alterselect.addAction(altercamera)
        alterselect.addAction(alterPhoto)
        alterselect.addAction(alterCancle)
        viewcontroller.present(alterselect, animated: true) {
            
        }
        
    }
   
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        //未经裁剪的图片   UIImagePickerControllerOriginalImage
         //通过key值获取到图片   UIImagePickerControllerEditedImage (裁剪之后的图片)
        let image:UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
        self.delegate?.getImageDelegate!(image: image)
        picker.dismiss(animated: true, completion: nil)
    }
    //系统方法压缩图片
    func compressionSystem(image:UIImage) -> NSData {
        var imageData = NSData.init()
        if (imageData.length>100*1024)
        {
            if (imageData.length>1024*1024) {//1M以及以上
                imageData = image.jpegData(compressionQuality: 0.1)! as NSData
                
            }else if (imageData.length>512*1024) {//0.5M-1M
                imageData = image.jpegData(compressionQuality: 0.5)! as NSData
                
            }else if (imageData.length>200*1024) {//0.25M-0.5M
                imageData = image.jpegData(compressionQuality: 0.9)! as NSData
                
            }
        }
        return imageData;
    }

}









